
package com.rx.mgt.service;  

import java.util.List;

import com.rx.mgt.util.StringUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.rx.mgt.dao.UserInfoExtDao;
import com.rx.mgt.domain.EmailUser;
import com.rx.mgt.domain.UserInfoExt;
import com.rx.mgt.po.UserGroupCountBo;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.util.DateUtil;
import com.rx.mgt.util.MD5;

/** 
 * ClassName:UserInfoExtService 
 * Function :
 * Reason   : 
 * Date     :Jan 31, 2017 9:21:36 PM 
 * @author   luolei 
 * @since    JDK 1.6
 */
@Service
public class UserInfoExtService {
    @Autowired
    UserInfoExtDao userInfoExtDao;
    @Autowired
    EmailUserService emailUserService;
    
    public Long getUserInfoExtCount(UsersBo usersBo){
        return userInfoExtDao.getUserInfoExtCount(usersBo);
    }
    @Transactional
    public UserInfoExt get(String id){
        return  userInfoExtDao.getById (id);
    }
    public List<UserGroupCountBo> getAllUserGroupCount(){
        return userInfoExtDao.getAllUserGroupCount();
    }
    @Transactional
    public List getAllUser(UsersBo usersBo){
    	return userInfoExtDao.getAllUser(usersBo);
    }
    @Transactional
    public String saveOrUpdate(UserInfoExt userInfoExt){
        if(userInfoExt.getUser_name()!=null){
            try {
                userInfoExtDao.update(userInfoExt);
            }catch (Exception e){
                e.printStackTrace();
            }

        }else{
           userInfoExtDao.save(userInfoExt);
        }
        return userInfoExt.getUser_name();
    }
    @Transactional
    public void delete(String id){
        UserInfoExt userInfoExt = userInfoExtDao.getById(id);
        if(userInfoExt!=null){
            userInfoExtDao.delete(userInfoExt);
        }
        EmailUser eu = emailUserService.selectUserByUserName(userInfoExt.getUser_name());
        if(eu!=null) {
            emailUserService.delete(userInfoExt.getUser_name());
        }
    }
    @Transactional
    public void stopUser(UserInfoExt userInfoExt){
        UserInfoExt info= userInfoExtDao.getById(userInfoExt.getId());
        if(info!=null){
            info.setStatus("N");
            saveOrUpdate(info);
            emailUserService.delete(info.getUser_name());
        }
    }
    @Transactional
    public void startUser(UserInfoExt userInfoExt){
        UserInfoExt info= userInfoExtDao.getById(userInfoExt.getId());
        if(info!=null){
            info.setStatus("Y");
            saveOrUpdate(info);
            EmailUser emailUser = emailUserService.get(info.getUser_name());
            if(emailUser==null){
                emailUser = new EmailUser();
                emailUser.setPassword(info.getPassword());
                emailUser.setUser_name(info.getUser_name());
                emailUserService.saveOrUpdate(emailUser);
            }
        }
    }
    @Transactional
    public void changeUserPsw(UserInfoExt userInfoExt){
        UserInfoExt info=null;
        if(userInfoExt.getId()!=null){
            info= userInfoExtDao.getById(userInfoExt.getId());
            if(info!=null){
                info.setPassword(Base64.encodeBase64String(userInfoExt.getPassword().getBytes()));
                EmailUser emailUser = emailUserService.get(info.getUser_name());
                if(emailUser!=null){
                    emailUser.setPassword(MD5.MD5Encode(userInfoExt.getPassword()));
                    emailUserService.saveOrUpdate(emailUser);
                }
                userInfoExtDao.update(info);
            }
        }
    }
    @Transactional
    public void changeUserInfo(UserInfoExt userInfoExt){
        UserInfoExt info=null;
        if(userInfoExt.getId()!=null){
            info= userInfoExtDao.getById(userInfoExt.getId());
            if(info==null){
                info = userInfoExtDao.getUserInfoByUserName(userInfoExt.getUser_name());
            }
            if(info!=null){
                info.setAddress(userInfoExt.getAddress());
                info.setDomain(userInfoExt.getDomain());
                info.setName(userInfoExt.getName());
                info.setUser_name(userInfoExt.getUser_name());
                info.setSex(userInfoExt.getSex());
                info.setTelephone(userInfoExt.getTelephone());
                info.setMax_mail_box_size(userInfoExt.getMax_mail_box_size());
                info.setMax_message_size(userInfoExt.getMax_message_size());
                info.setRemark(userInfoExt.getRemark());
                info.setGroup_id(userInfoExt.getGroup_id());
                info.setWork_no(userInfoExt.getWork_no());
                userInfoExtDao.saveOrUpdate(info);
                EmailUser emailUser = emailUserService.get(info.getUser_name());
                if(emailUser!=null){
                    String pass = info.getPassword();
                    emailUser.setPassword(MD5.MD5Encode(StringUtil.decodeBase64(pass)));
                    emailUserService.saveOrUpdate(emailUser);
                }else{
                    emailUser = new EmailUser();
                    emailUser.setPassword(MD5.MD5Encode(StringUtil.decodeBase64(info.getPassword())));
                    emailUser.setUser_name(info.getUser_name());
                    emailUser.setPassword_hash_algorithm("MD5");
                    emailUser.setVersion(1);
                    emailUserService.saveOrUpdate(emailUser);
                }
               
            }else{
                userInfoExt.setId(StringUtil.UUID());
                userInfoExt.setCreate_time(DateUtil.nowTimeString());
                userInfoExt.setStatus("Y");
                String pass = userInfoExt.getPassword();
                userInfoExt.setPassword(Base64.encodeBase64String(pass.getBytes()));
                userInfoExtDao.save(userInfoExt);
                EmailUser emailUser = new EmailUser();
                emailUser.setPassword(MD5.MD5Encode(pass));
                emailUser.setUser_name(userInfoExt.getUser_name());
                emailUser.setPassword_hash_algorithm("MD5");
                emailUser.setVersion(1);
                emailUserService.saveOrUpdate(emailUser);
            }
        }else{
            userInfoExt.setId(StringUtil.UUID());
            userInfoExt.setStatus("Y");
            String pass = userInfoExt.getPassword();
            userInfoExt.setPassword(Base64.encodeBase64String(pass.getBytes()));
            userInfoExt.setCreate_time(DateUtil.nowTimeString());
            userInfoExtDao.save(userInfoExt);
            EmailUser emailUser = new EmailUser();
            emailUser.setPassword(MD5.MD5Encode(pass));
            emailUser.setUser_name(userInfoExt.getUser_name());
            emailUser.setPassword_hash_algorithm("MD5");
            emailUser.setVersion(1);
            emailUserService.saveOrUpdate(emailUser);
        }
    }
    
    @Transactional
    public void batchRegist(UserInfoExt userInfoExt){
        EmailUser user = emailUserService.get(userInfoExt.getUser_name());
        if(user!=null){
            return ;
        }

        userInfoExt.setId(StringUtil.UUID());
        String pass = userInfoExt.getPassword();
        userInfoExt.setPassword(Base64.encodeBase64String(pass.getBytes()));
      //  userInfoExt.setStatus("Y");
        userInfoExtDao.save(userInfoExt);
        EmailUser emailUser = new EmailUser();
        emailUser.setPassword(MD5.MD5Encode(pass));
//        emailUser.setPassword(userInfoExt.getPassword());
        emailUser.setUser_name(userInfoExt.getUser_name());
        emailUser.setPassword_hash_algorithm("MD5");
        emailUser.setVersion(1);
        emailUserService.saveOrUpdate(emailUser);
    }


    @Transactional
    public int batchUpdate(List<UserInfoExt> list) {
        int i= 1;
        for(UserInfoExt ext :list){
            UserInfoExt ex = userInfoExtDao.getUserInfoByUserName(ext.getUser_name());
            if(ex==null){
                continue;
            }
            String pass = ext.getPassword();
            ex.setPassword(Base64.encodeBase64String(pass.getBytes()));
            ex.setDomain(ext.getDomain());
            ex.setMax_message_size(ext.getMax_message_size());
            ex.setWork_no(ext.getWork_no());
            ex.setMax_mail_box_size(ext.getMax_mail_box_size());
            ex.setStatus(ext.getStatus());
            ex.setName(ext.getName());
            userInfoExtDao.update(ex);
            EmailUser emailUser = new EmailUser();
            emailUser.setPassword(pass);
//        emailUser.setPassword(userInfoExt.getPassword());
            emailUser.setUser_name(ext.getUser_name());
            emailUser.setPassword_hash_algorithm("MD5");
            emailUser.setVersion(1);
            emailUserService.saveOrUpdate(emailUser);
        }
        return i;
    }

    @Transactional(readOnly = true)
    public UserInfoExt getUserInfoByUserName(String email) {
       return userInfoExtDao.getUserInfoByUserName(email);
    }

    @Transactional
    public List<UserInfoExt> getAllUserHasNikeName(UsersBo usersBo) {
        return userInfoExtDao.getAllUserHasNikeName(usersBo);
    }

    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public String updateEmailAlias(UserInfoExt infoExt) {
        UserInfoExt info =userInfoExtDao.getById(infoExt.getId());
        info.setNike_name(infoExt.getNike_name());
         userInfoExtDao.saveOrUpdate(info);
        return infoExt.getUser_name();
    }

    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public Integer deleteEmailAlias(String id) {
        return userInfoExtDao.deleteEmailAlias(id);

    }

    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public Integer emailAliasSave(UserInfoExt infoExt) {
        UserInfoExt userInfoByUserName = userInfoExtDao.getUserInfoByUserName(infoExt.getUser_name());
        if(userInfoByUserName==null){
            return 0;
        }else{
            userInfoByUserName.setNike_name(infoExt.getNike_name());
            userInfoExtDao.saveOrUpdate(userInfoByUserName);
            if(StringUtils.isNotBlank(userInfoByUserName.getUser_name())){
                return 1;
            }else{
                return 0;
            }

        }
    }
    /**根据邮箱别名获取邮件列表**/
    public UserInfoExt getUserInfoByNickName(String nickName) {
        return userInfoExtDao.getUserInfoByNickName(nickName);
    }
}
  